<template>
  <div class="pagination">
    <ul class="pager clear" @click="onPage">
      <li
        :class="{ disabled: page <= 1 }"
        :aria-disabled="page <= 1"
        :arial-label="arialLabel(-1)"
        tabindex="0"
        class="pn prev hide_m"
        data-page="1"
        role="button"
      >
        <span class="arrow">首页</span>
      </li>
      <li
        :class="{ disabled: page <= 1 }"
        :aria-disabled="page <= 1"
        :arial-label="arialLabel(-1)"
        tabindex="0"
        class="pn prev"
        data-page="-1"
        role="button"
      >
        <span class="arrow">上一页</span>
      </li>
      <template v-for="(group, index) in slices">
        <li
          v-if="group[0] === -1"
          :key="'g' + index"
          :data-page="group[1]"
          :data-jumper="index"
          :arial-label="arialLabel(group[1])"
          class="pn jumper"
        >
          <span class="dots">...</span>
        </li>
        <li
          v-for="i in group"
          v-else
          :key="'l' + i"
          :class="{ active: page === i }"
          :data-page="i"
          :arial-label="arialLabel(i)"
          class="pn"
          role="button"
        >
          <span>{{ i }}</span>
        </li>
      </template>
      <li
        :class="{ disabled: page >= page_c }"
        :aria-disabled="page >= page_c"
        :arial-label="arialLabel(0)"
        tabindex="0"
        class="pn next"
        data-page="0"
        role="button"
      >
        <span class="arrow">下一页</span>
      </li>
      <li
        :class="{ disabled: page >= page_c }"
        :aria-disabled="page >= page_c"
        :arial-label="arialLabel(0)"
        tabindex="0"
        class="pn next hide_m"
        :data-page="page_c"
        role="button"
      >
        <span class="arrow">尾页</span>
      </li>
    </ul>
    <div v-if="showSizes" class="page-size">
      每页显示数量
      <div
        class="page-select"
        @mouseenter="showPageList = true"
        @mouseleave="showPageList = false"
      >
        {{ pageSize }}
        <div v-if="showPageList" class="select-box">
          <div
            v-for="(item, index) in pageSizeList"
            :key="index"
            class="seleclt-opotion"
            @click.stop="onSize(item)"
          >
            {{ item }}
          </div>
        </div>
      </div>
      共{{ total }}条
    </div>
  </div>
</template>

<script lang="ts" setup>
const props = defineProps({
  page: {
    type: Number,
    default: 1,
  },
  total: {
    type: Number,
    default: 0,
  },
  pageSize: {
    type: Number,
    default: 10,
  },

  // 选择分页size
  showSizes: {
    type: Boolean,
    default: false,
  },
  // 页码list
  pageSizeList: {
    type: Array,
    default: [10, 20],
  },
});
const page_c = ref(0);
const slices = ref([[1]]);
const showPageList = ref(false);
const emits = defineEmits(["onPageChange", "onPageSizeChange"]);
onMounted(() => {
  updateSlices();
});
const updateSlices = () => {
  const pages = Math.ceil(props.total / props.pageSize);
  page_c.value = pages;
  if (pages < 5) {
    slices.value = [
      Array(pages)
        .fill(1)
        .map((o, i) => i + 1),
    ];
  } else if (props.page < 4) {
    slices.value = [[1, 2, 3], [-1, 4], [pages]];
  } else if (pages - props.page < 3) {
    slices.value = [[1], [-1, 2], [pages - 2, pages - 1, pages]];
  } else {
    slices.value = [
      [1],
      [-1, 2],
      [props.page - 1, props.page, props.page + 1],
      [-1, props.page + 2],
      [pages],
    ];
  }
};
watch([() => props.page, () => props.pageSize, () => props.total], (nv) => {
  updateSlices();
});

const onSize = (e: any) => {
  emits("onPageSizeChange", e);
  //const page = Math.ceil(this.total / e);
  emits("onPageChange", 1);
  showPageList.value = false;
};

const calcNextPage = (page: number) => {
  return !page ? props.page + 1 : page < 0 ? props.page - 1 : page;
};
const onPage = (e: any) => {
  let target = e.target;
  if (target.tagName === "SPAN") {
    target = target.parentElement;
  }
  if (target.className.indexOf("disabled") !== -1 || target.tagName !== "LI") {
    return;
  }

  const page = +target.getAttribute("data-page");
  const jumper = target.getAttribute("data-jumper");
  // console.log("page", page, "jumper", jumper);
  if (jumper) {
    // this.showJumper(+jumper, target)
    emits("onPageChange", page);
  } else {
    emits("onPageChange", calcNextPage(page));
  }
};
const arialLabel = (i: number) => {
  if (i === -1) {
    return "上一页";
  }
  if (i === 0) {
    return "下一页";
  }
  return `第${i}页`;
};
const howJumper = (num: number, target: any) => {
  if (num && num > 0 && num <= page_c.value) {
    const slice = [...slices.value];
    slice[num][2] = 1;
    slices.value = slice;
    setTimeout(() => {
      target.children[1].focus();
    }, 100);
  }
};
const onJump = (e: any) => {
  console.log(e.target.value);
  const val = +e.target.value;
  if (val && val > 0 && val <= page_c.value) {
    emits("onPageChange", val);
  }
};
const onBlur = (e: any) => {
  const num = +e.target.parentNode.getAttribute("data-jumper");
  //   const slices = [...this.slices];
  //   slices[num][2] = 0;
  //   slices.value = slices;
};
</script>
<style lang="scss">
$size: 1.6rem;
$fsize: 1.1rem;
$arrow: 6px;
$theme: blue;
.pagination {
  font-size: $fsize;
  color: #999;
  letter-spacing: 1.8px;
  font-weight: 400;
  line-height: $size;
  display: flex;
  align-items: center;
  justify-content: center;

  margin-top: 50px;
  text-align: center;
  & > .pager {
    display: inline-block;
    white-space: nowrap;
  }
  .pn {
    float: left;
    cursor: pointer;
    width: $size;
    height: $size;
    line-height: $size;
    margin-left: 4px;
    text-align: center;
    border: 1px solid $theme;
    border-radius: 2px;
    font-size: $fsize;
    border-radius: 6px;
    letter-spacing: 1.8px;
    font-weight: 400;
    outline: none;
    display: block;

    &:first-child {
      margin-left: 0;
    }
    &:hover:not(.disabled) {
      background-color: $theme;
      color: #fff;
    }
    &.active.active {
      background-color: $theme;
      border-color: $theme;
      color: #fff;
    }
    & > .dots {
      display: block;
      font-weight: bold;
      line-height: 1.2rem;
      padding-bottom: 6px;
    }
    & > input {
      color: #666;
      border: 0;
      font-family: Arial, sans-serif;
      font-size: 15px;
      max-width: 30px;
      padding: 2px 1px;
    }
    &.prev,
    &.next {
      display: flex;
      width: auto;
      padding: 0 8px;
      border-radius: 4px;
      align-items: center;
      justify-content: center;
    }
    &.prev {
      .arrow {
      }
    }
    &.next {
      .arrow {
      }
    }
    &.next,
    &.prev {
    }
    &.disabled {
      cursor: not-allowed;
      color: #999 !important;
    }
  }
  @media only screen and (max-width: 750px) {
    .pn {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 12px;
      margin-left: 10px;
      .dots {
        line-height: 20px;
      }
    }
  }
  & > .elevator {
    display: inline-block;
    color: #888f9c;
    font-size: 14px;
    height: 30px;
    line-height: 30px;
    margin-left: 20px;
    position: relative;
    vertical-align: top;

    & > .pagenum {
      appearance: none;
      background: transparent;
      color: #666;
      border: 1px solid #e6e7eb;
      border-radius: 0;
      font-size: 18px;
      margin: 0 10px 0 2px;
      padding-left: 10px;
      width: 60px;
      height: 30px;
    }

    & > .icn {
      display: block;
      border-top: 6px solid #5c5c5c;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-bottom: none;
      pointer-events: none;
      position: absolute;
      top: 17px;
      left: 142px;
    }
  }
  .page-size {
    margin-left: 30px;
    display: flex;
    align-items: center;
    .page-select {
      height: 30px;
      background: #f6f7fc;
      min-width: 55px;
      padding: 0 8px;
      margin: 0 8px;
      border: none;
      outline: none;
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
      cursor: pointer;
      &::after {
        content: "";
        width: 0px;
        display: block;
        height: 0px;
        line-height: 0px;
        margin-left: 6px;
        border-top: 6px solid #d8d8d8;
        border-left: 6px solid #f6f7fc;
        border-right: 6px solid #f6f7fc;
      }
      .select-box {
        position: absolute;
        left: 0;
        width: 100%;
        top: 30px;
        border: 1px solid #eee;
        .seleclt-opotion {
          padding: 0 10px;
          &:hover {
            background: #f6f7fc;
          }
        }
      }
    }
  }
}
</style>
